﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Icoders.MarketTracker.Web.Lib.Command.Orders;
using Icoders.MarketTracker.Web.Lib.Command.Transactions.Orders;
using Icoders.MarketTracker.Web.Lib.CommandHandler.Orders;

namespace Icoders.MarketTracker.Web.Lib.Command.Helper.Impl
{
    public class ResolveCommand : IResolveCommand
    {
       public  ResolveCommand()
        {
                Init();
        }
       private List<ResolveCommandItem> _items;

       void Init()
       {
           _items = new List<ResolveCommandItem>();
           //----------- Orders --------------------------
           _items.Add(Create("CreateOrder", CommandType.CreateOrder, typeof (CreateOrderCommand),typeof (ICreateOrderCommandHandler)));
           _items.Add(Create("AddOrderLineItem", CommandType.AddOrderLineItem, typeof(AddOrderLineItemCommand), typeof(IAddOrderLineItemCommandHandler)));
           _items.Add(Create("ConfirmOrder", CommandType.ConfirmOrder, typeof(ConfirmOrderCommand), typeof(IConfirmOrderCommandHandler)));
       }
       ResolveCommandItem Create(string commandName, CommandType commandType, Type command, Type commandHandler)
       {
           return new ResolveCommandItem { CommandName = commandName, CommandType = commandType, Command = command, CommandHandlerContract = commandHandler };
       }

       public ResolveCommandItem Get(string commandName)
       {
           return _items.FirstOrDefault(n => n.CommandName == commandName);
       }

       public ResolveCommandItem Get(CommandType commandType)
       {
           return _items.FirstOrDefault(n => n.CommandType == commandType);
       }

       public ResolveCommandItem Get(ICommand command)
       {
           ResolveCommandItem rv = null;
           foreach (var item in _items)
           {
               if (command.GetType() == item.Command)
                   rv = item;
           }
           return rv;
       }
    }
}
